package sample.client;

import java.util.ArrayList;

import sample.client.view.gxt.Document;
import sample.shared.Consts;
import sample.shared.result.Client;
import sample.shared.result.FileInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.info.Info;

public class DocumentList
{
  private final ClientManagerServiceAsync cmService = GWT
      .create(ClientManagerService.class);
  
  private Client client;
  private ListStore<Document> store;

  public DocumentList(Client client, final ListStore<Document> store) {
    this.client = client;
    this.store = store;
    update();
  }

  public void update() {
    cmService.getFiles(client, new AsyncCallback<ArrayList<FileInfo>>() {
      @Override
      public void onFailure(Throwable caught) {
        Info.display("Error", "Failed to obtain client files" + Consts.NEWLINE
            + caught.getMessage());
      }

      @Override
      public void onSuccess(ArrayList<FileInfo> result) {
        store.clear();
        for (FileInfo f : result) {
          store.add(new Document(f));
        }
      }
    });
  }
}
